//
//  FPUserIdentifierCardInfoModel.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/2/5.
//

#import "FPUserIdentifierCardInfoModel.h"

@implementation FPUserIdentifierCardInfoModel

+ (JSONKeyMapper *)keyMapper {
    
    return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{@"ID" : @"id"}];
}

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
    
    return YES;
}

+ (void)getInfoWithIDCardURL:(NSString *)url success:(void (^)(NSInteger , NSString *, FPUserIdentifierCardInfoModel *))success failure:(void (^)(NSError *error))failure {
    
    NSMutableDictionary *param = APIClient.creatAPIDictionary;
    [param setObject:@"front" forKey:@"side"];
    [param setObject:url forKey:@"url"];
    
    NSString *URLString = [APIClient getQueryURLStringWithParam:param URLString:UPLOAD_PHOTO_TRANSFORM_TEXT_URL]; // 旧 UPLOAD_IDENTIFIER_CARD_GET_INFO_URL
//    NSString *URLString = [APIClient getQueryURLStringWithParam:param URLString:UPLOAD_PHOTO_TRANSFORM_TEXT_URL]; // 旧 UPLOAD_IDENTIFIER_CARD_GET_INFO_URL
    
    [[APIClient sharedInstance] requestWithURLString:[MAIN_URL stringByAppendingString:URLString] parameters:@{} type:HttpRequestTypePost success:^(id responseObject, NSString *msg, NSInteger code) {
        
        NSError *error;
        
        id object = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        
        FPUserIdentifierCardInfoModel *model = [[FPUserIdentifierCardInfoModel alloc] initWithDictionary:[object objectForKey:@"data"] error:&error];
        
        NSInteger httpCode = [[object objectForKey:@"code"] integerValue];
        
        NSString *message = [object objectForKey:@"msg"];
        
        if (success) {
            
            success(httpCode , message , model);
        }
        
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)duplicateIdentifierWithIDCardNumber:(NSString *)number success:(void (^)(NSInteger, NSString * _Nonnull, BOOL, BOOL, NSString * _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure {
    
    NSMutableDictionary *param = NSMutableDictionary.dictionary;
    
    [param setObject:number forKey:@"idCard"];
    
    [[APIClient sharedInstance] requestWithURLString:[MAIN_URL stringByAppendingString:IDENTIFIER_DUPLICATE_URL] parameters:param type:HttpRequestTypeGet success:^(id responseObject, NSString *msg, NSInteger code) {
        
        NSError *error;
        
        id object = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        
        NSInteger httpCode = [[object objectForKey:@"code"] integerValue];
        
        NSString *message = [object objectForKey:@"msg"];
        
//        BOOL duplicate = [[object objectForKey:@"data"][@"isRepeat"] boolValue];
//        BOOL ageEnough = [[object objectForKey:@"data"][@"age"] integerValue] < 22 ? NO : YES;
//        NSString *phone;
//        
//        NSArray *arr = [object objectForKey:@"data"][@"phone"];
        
        BOOL duplicate = [[[object objectForKey:@"data"] objectForKey:@"isRepeat"] boolValue];
        BOOL ageEnough = [[[object objectForKey:@"data"] objectForKey:@"age"] integerValue] < 22 ? NO : YES;
        NSString *phone;
        
        NSArray *arr = [[object objectForKey:@"data"] objectForKey:@"phone"];
        
        
        
        
        if (arr.count > 0) {
            
            phone = [FPUserModel AES256Decrypt:arr.firstObject];
        }
        
        if (success) {
            
            success(httpCode , message , duplicate , ageEnough , phone);
        }
        
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}


@end
